// map.js
import qqmap from './../../../utils/qqmap'

Page({
  data: {
    id: '',
    latitude: '',
    longitude: '',
    /** 地址信息 */
    address: '中国广州吉山幼儿园',
    repairInfo: {}
  },

  onLoad(ev) {
    // console.log(ev.id);
    this.setData({
      id: ev.id
    })
    this.getDetail()
    const { address } = this.data
    this.geocoder(address)
  },

  /** 查询报修详情信息 */
  async getDetail() {
    let res = await wx.http({
      url: '/repair/' + this.data.id
    })
    // console.log(res.data);
    this.setData({
      repairInfo: res.data
    })
  },

  /** 地址解析 */
  geocoder(address) {
    qqmap.geocoder({
      address,
      success: (res) => {
        console.log(res.result.location);
        this.setData({
          latitude: res.result.location.lat,
          longitude: res.result.location.lng,
        })
      }
    })
  },

  /** 跳转修改信息 */
  goRepairForm() {
    wx.reLaunch({
      url: '/repair_pkg/pages/form/index?id=' + this.data.id
    })
  },

  /** 点击取消报修，弹窗确认，发请求，成功后跳转报修列表页面 */
  cancelRepair() {
    wx.showModal({
      title: '提示',
      content: '您确定要取消吗',
      success: async (res) => {
        if (res.confirm) {
          // console.log('确定');
          let res = await wx.http({
            url: '/cancel/repaire/' + this.data.id,
            method: 'put'
          })
          // console.log(res);

          if (res.code !== 10000) {
            return wx.utils.toast(res.message)
          }

          wx.showToast({
            title: '取消报修成功',
            icon: 'success'
          })

          setTimeout(() => {
            wx.reLaunch({
              url: '/repair_pkg/pages/list/index'
            })
          }, 1500)
        }
      }
    })
  }

})
